<?php
require_once ('core_model.php');

/**
 * 电子书
 * @author kisskizz
 * @version 1.0
 * @created 09-六月-2008 20:31:10
 */
class Ebook_model extends Core_model
{
	
	function __construct() {
		$this->_table = "thankphp_ebook";
	}

	function __destruct()
	{
	}



	/**
	 * 
	 * @param data
	 */
	public function create(array $data) {
		parent::create($data);
	}

	/**
	 * 
	 * @param data
	 */
	public function updated(array $data) {

	}
	

	public function show($catagory_id) {
		if($catagory_id) {
			$search = array(
				'catagory_id'=>$catagory_id
			);
		} else {
			$search = array();
		}
		$this->load->model("catagory_model","catagory");
		$data['menu'] = array("menus"=>$this->catagory->getMenus(),"id"=>$catagory_id);
		$data["ebook_list"] = array("ebooks"=>$this->retriveby('',$search));
		$this->display->mainpage($data);
		unset($data);
	}
	
	function getAllBooks() {
		$sql = sprintf("select qte.id,qte.ebook_name,qtc.catagory_name,ebook_path,ebook_face,memo,qte.`create`,qte.`update` from `%s` qte left join `%s` qtc on qte.catagory_id = qtc.id","qj_".$this->_table,"qj_thankphp_catagory");
		return $this->_getBooks($sql);
	}

	function getBooks($page = 1 ,$orderby = '' ,$num = 10,$where='') {
		$sql = sprintf("select qte.id,qte.ebook_name,qtc.catagory_name,ebook_path,ebook_face,memo,qte.`create`,qte.`update` from `%s` qte left join `%s` qtc on qte.catagory_id = qtc.id %s order by %s limit %d,%d","qj_".$this->_table,"qj_thankphp_catagory",$where,$orderby,($page-1)*$num,$num);
		$rows = $this->_getBooks($sql);
		$t = array();
		$i= ($page-1)*10+1;
		foreach($rows as $row) {
			$t[] = array("id"=>$row['id'],"cell"=>array($i,$row['ebook_name'],$row['catagory_name'],$row['ebook_path'],$row['ebook_face'],$row['memo'],$row['create']));
			$i++;
		}
		return $t;
	}

	function getNums($where= '') {
		$sql = sprintf("select count(1) number from `%s` qte left join `%s` qtc on qte.catagory_id = qtc.id %s" , "qj_".$this->_table,"qj_thankphp_catagory",$where);
		$q = $this->db->query($sql);
		if($q->num_rows()>0) {
			$row = $q->row_array();
			return $row['number'];
		} return 0;
	}

	function _getBooks($sql) {
		$q = $this->db->query($sql);
		if($q->num_rows()>0) {
			return $q->result_array();
		} return false;
	}

	function admin() {
		$data = array("ebook"=>array("ebooks"=>$this->getAllBooks()));
		return $data;
	}
}
?>